<?php
/**
 * Created by PhpStorm.
 * User: kc
 * Date: 2017/4/14
 * Time: 15:57
 */
class CarController extends BaseController{
    /**
     * @Author:      kc
     * @DateTime:    2017-04-14 16:42:29
     * @Description: 车辆品牌
     */
    public function carBrand(){
        do{
            //热门品牌数据
            $hot_cars = DataCarBrand::where('is_hot',1)
                ->where('status',1)
                ->select(array('id','initial','name','icon'))
                ->orderBy('sort', 'asc')
                ->get();
            //获取品牌数据
            $initials = DB::table('anhui_data.car_brand')->where('status',1);
                if(Input::get('brand_name')){
                    $initials->where('name','like','%'.Input::get('brand_name').'%');
                }
            $normal_cars = $initials->select('initial')->groupBy('initial')->get();
            foreach ($normal_cars as $v) {
                $brand =DB::table('anhui_data.car_brand')->where('status',1);
                if(Input::get('brand_name')){
                    $brand->where('name','like','%'.Input::get('brand_name').'%');
                }
                $car_brand = $brand->where('initial',$v->initial)
                    ->select(array('id','initial','name','icon'))
                    ->get();
                $v->brand=$car_brand;
            }
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array(
                    'car_brand_img_url' =>  Config::get('config.car_brand_img_url'),
                    'hot_cars' => $hot_cars,
                    'car_brand' => $normal_cars,
                )
            );
        }while(false);
        Log::info("info", array("system" => "wechat2.0" ,"event" => "carBrand", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:    2017-01-17 10:00:33
     * @Description: 车辆系列
     */
    public function carSeries(){
        $validator = Validator::make(Input::all(),
                array(
                    'car_brand_id' => 'required',
                )
            );
        do{
            if($validator->fails()){
                $response = Lang::get("error.args_error_10000");
                $response['result'] = $validator->messages()->all();
                break;
            }
            $car_models = DataCarSeries::join('anhui_data.car_standard_info','car_standard_info.series_name_id','=','car_series.id')
                ->where('car_series.status',1)
                ->where('car_standard_info.status',1)
                ->where('car_series.car_brand_id',Input::get('car_brand_id'))
                ->select(array('car_series.id','car_series.name','car_series.icon','car_standard_info.series_shop','car_standard_info.series_shop_id'))
                ->groupBy('car_series.id')
                ->get();
            $car_series_menu = array();
            foreach($car_models as $k=>$v){
                $temp_arr = array();
                $temp_arr['name'] =  $v->name;
                $temp_arr['id'] = $v->id;
                $temp_arr['icon'] = $v->icon;
                $car_series_menu[$v->series_shop_id]['series_shop'] = $v->series_shop;
                $car_series_menu[$v->series_shop_id]['series_shop_id'][] = $temp_arr;
            }
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $car_series_menu,
            );
        }while(false);
        Log::info("info", array("system" => "wechat2.0" ,"event" => "carModel", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      dongxia
     * @DateTime:    2017-05-08 16:45:29
     * @Description: 根据车品牌获取车系
     */
    public function series(){
        $validator = Validator::make(Input::all(),
            array(
                'car_brand_id' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                break;
            }

            //获取品牌车辆的厂商
            $factories = DataCarStandardInfo::where('brand_name_id',Input::get('car_brand_id'))
                ->select(array('series_shop_id','series_shop'))
                ->distinct()
                ->get();
            $car_factories = array();
            if(!is_null($factories)){
                foreach ($factories as $_factory) {
                    $series = DataCarStandardInfo::where('brand_name_id',Input::get('car_brand_id'))
                        ->where('series_shop_id',$_factory->series_shop_id)
                        ->select(array('series_name_id as id','series_name as name'))
                        ->distinct()
                        ->get()->toArray();
                    $factory = array(
                        'factory' => $_factory->series_shop,
                        'shop_id' => $_factory->series_shop_id,
                        'series' => $series
                    );
                    array_push($car_factories,$factory);
                }
            }
            $result = array(
                'series_shop' => $car_factories
            );
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "wechat2.0" ,"event" => "series", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      kc
     * @DateTime:    2017-04-17 10:45:29
     * @Description: 车辆排量
     */
    public function gasRate(){
        $validator = Validator::make(Input::all(),
            array(
                'series_id' => 'required',
                'series_shop_id' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get("error.args_error_10000");
                $response['result'] = $validator->messages()->all();
                break;
            }
            $gear_box =DataCarStandardInfo::where('series_shop_id',Input::get('series_shop_id'))
                ->where('series_name_id',Input::get('series_id'))
                ->where('status',1)
                ->select('model_gas_rate')
                ->distinct()
                ->get();
            $response = array(
                "code" => 0,
                "msg" => "ok",
                "result" => $gear_box,
            );
        }while(false);
        Log::info("info", array("system" => "wechat2.0" ,"event" => "gearBox", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:    2017-04-17 11:12:44
     * @Description: 车辆年款
     */
    public function modelYear(){
        $validator = Validator::make(Input::all(),
            array(
                'series_id' => 'required',
                'series_shop_id' => 'required',
                'model_gas_rate' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get("error.args_error_10000");
                $response['result'] = $validator->messages()->all();
                break;
            }
            $model_year = DataCarStandardInfo::where('series_shop_id',Input::get('series_shop_id'))
                ->where('series_name_id',Input::get('series_id'))
                ->where('model_gas_rate',Input::get('model_gas_rate'))
                ->where('status',1)
                ->select('model_year')
                ->distinct()
                ->get();
            $response = array(
                "code" => 0,
                "msg" => "ok",
                "result" => $model_year,
            );
        }while(false);
        Log::info("info", array("system" => "wechat2.0" ,"event" => "modelYear", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:    2017-04-17 13:40:28
     * @Description: 车辆型号
     */
    public function modelName(){
        $validator = Validator::make(Input::all(),
            array(
                'series_id' => 'required',
                'series_shop_id' => 'required',
                'model_gas_rate' => 'required',
                'model_year' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get("error.args_error_10000");
                $response['result'] = $validator->messages()->all();
                break;
            }
            $model_name = DataCarStandardInfo::where('series_shop_id',Input::get('series_shop_id'))
                ->where('series_name_id',Input::get('series_id'))
                ->where('model_gas_rate',Input::get('model_gas_rate'))
                ->where('model_year',Input::get('model_year'))
                ->where('status',1)
                ->select([
                    'model_name',
                    'id',
                    'brand_name_id as brand_id',
                    'series_shop_id',
                    'series_name_id',
                ])
                ->get();
            $response = array(
                "code" => 0,
                "msg" => 'ok',
                "result" => $model_name
            );
        }while(false);
        Log::info("info", array("system" => "wechat2.0" ,"event" => "modelName", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}